package com.mission.shaco.warehouse.models

import android.content.ContentValues.TAG
import android.content.SharedPreferences
import android.os.Environment
import android.util.Log
import com.mission.shaco.warehouse.models.ClientConfig.FILES_PATH
import org.json.JSONArray
import org.json.JSONObject
import java.io.*
import java.util.ArrayList
import org.json.JSONException



/**
 * Created by Administrator on 2017/6/12.
 */
class Tool {
    public  fun JSONRemove(idx: Int, from: JSONArray): JSONArray {
        val objs = asList(from)
        objs.removeAt(idx)

        val ja = JSONArray()
        for (obj in objs) {
            ja.put(obj)
        }

        return ja
    }

    public fun asList(ja: JSONArray): MutableList<JSONObject> {
        val len = ja.length()
        val result = ArrayList<JSONObject>(len)
        for (i in 0..len - 1) {
            val obj = ja.optJSONObject(i)
            if (obj != null) {
                result.add(obj)
            }
        }
        return result
    }

    public fun asList(jo : JSONObject): List<String>{
        val len = jo.length()
        val result = ArrayList<String>(len)
        for(key in jo.keys() ){
            var obj = jo.getString(key)
            if (obj != null) {
                result.add(obj)
            }
        }
        return result
    }

    @Throws(JSONException::class)
    fun concatArray(arr1: JSONArray, arr2: JSONArray): JSONArray {
        val result = JSONArray()
        for (i in 0..arr1.length() - 1) {
            result.put(arr1.get(i))
        }
        for (i in 0..arr2.length() - 1) {
            result.put(arr2.get(i))
        }
        return result
    }

    @Throws(JSONException::class)
    fun concatArray(arr1: JSONArray, arr2: JSONArray, arr3:JSONArray ): JSONArray {
        val result = JSONArray()
        for (i in 0..arr1.length() - 1) {
            result.put(arr1.get(i))
        }
        for (i in 0..arr2.length() - 1) {
            result.put(arr2.get(i))
        }
        for (i in 0..arr3.length() - 1) {
            result.put(arr3.get(i))
        }
        return result
    }

    private fun getSerializableFilePath(fileName:String):String{
        return this.getFilePath()+this.getFileName(fileName)
    }

    private fun getFileName(fileName:String) :String{
        return fileName+".ser"
    }

    private fun getFilePath():String{
        return FILES_PATH
    }

    public fun serializableObject(fileName:String, obj: Any): String {

        val path :String = getSerializableFilePath(fileName)

        val dir = File(getFilePath())
        if (!dir.exists()) {
            dir.mkdirs()
        }

        val file = File(dir, getFileName(fileName))
        if (!file.exists()) {
            file.createNewFile()
        }else{
            file.delete()
            file.createNewFile()
        }

        val fout = FileOutputStream(path, true)
        val oos = ObjectOutputStream(fout)

        try {
            oos.reset()
            oos.writeObject(obj)
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return path
    }


    public fun <T : Serializable> getObjectFromString(fileName: String, clazz: Class<T>): T? {

        val path :String = getSerializableFilePath(fileName)

        val file = File(path)
        val length = file.length()
        if (!file.exists() || !(length > 0) ) {
            return null
        }

        val streamIn =  FileInputStream(getSerializableFilePath(fileName))
        val objectinputstream =  ObjectInputStream(streamIn)
        var readCase:T? = null

        try {
            readCase =  objectinputstream.readObject() as T
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: ClassNotFoundException) {
            e.printStackTrace()
        }

        return readCase
    }



}


